package br.com.evento.view.mbean;

import java.util.Collections;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import br.com.evento.business.EventoBO;
import br.com.evento.exception.BOException;
import br.com.evento.model.entity.Evento;
import br.com.evento.model.entity.Usuario;
import br.com.evento.util.FacesUtil;

/**
 * ManagedBean utilizado para a tela Calandario Google
 * 
 * @author Rafael Trassi
 */
@ManagedBean(name="scheduleController")
@ViewScoped
public class ScheduleController implements MBean {

	private static final long serialVersionUID = 1L;
	private ScheduleModel eventModel;
	private ScheduleModel lazyEventModel;
	private ScheduleEvent event = new DefaultScheduleEvent();
	private String theme;
	
	private EventoBO eventoBO;
	private List<Evento> eventos;
	@SuppressWarnings("unused")
	private Evento evento;
	private Usuario usuario;

	/**
	 * Construtor inicializa o calendario com os eventos
	 */
	public ScheduleController() {
		
		eventoBO = new EventoBO();
		usuario = ((UsuarioMBean)FacesUtil.getMBean(FacesContext.getCurrentInstance(), "usuarioMBean")).getUsuario();
		refreshList();
		
		eventModel = new DefaultScheduleModel();
		
		for (Evento e : eventos) {
			eventModel.addEvent(new DefaultScheduleEvent(e.getTitulo(), e.getDataHora(), e.getDataHora()));
		}
		
		/*lazyEventModel = new LazyScheduleModel() {
			
			@Override
			public void fetchEvents(Date start, Date end) {
				clear();
				
				Date random = getRandomDate(start);
				addEvent(new DefaultScheduleEvent("Lazy Event 1", random, random));
				
				random = getRandomDate(start);
				addEvent(new DefaultScheduleEvent("Lazy Event 2", random, random));
			}	
		};*/
	}
	
	public void addEvent(ActionEvent actionEvent) {
		if(event.getId() == null)
			eventModel.addEvent(event);
		else
			eventModel.updateEvent(event);
		
		event = new DefaultScheduleEvent();
	}
	
	public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
		event = selectEvent.getScheduleEvent();
	}
	
	public void onDateSelect(DateSelectEvent selectEvent) {
		event = new DefaultScheduleEvent(Math.random() + "", selectEvent.getDate(), selectEvent.getDate());
	}
	
	public void onEventMove(ScheduleEntryMoveEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
		
		addMessage(message);
	}
	
	public void onEventResize(ScheduleEntryResizeEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
		
		addMessage(message);
	}
	
	private void addMessage(FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	/**
	 * Atualiza a lista de eventos
	 */
	public void refreshList() {
		try {
			eventos = eventoBO.getEventosByUsuario(usuario);
			Collections.sort(eventos);
		} catch (BOException e) {
			FacesUtil.setMsgErro(e);
		}
	}

	public ScheduleModel getEventModel() {
		return eventModel;
	}

	public void setEventModel(ScheduleModel eventModel) {
		this.eventModel = eventModel;
	}

	public ScheduleModel getLazyEventModel() {
		return lazyEventModel;
	}

	public void setLazyEventModel(ScheduleModel lazyEventModel) {
		this.lazyEventModel = lazyEventModel;
	}

	public ScheduleEvent getEvent() {
		return event;
	}

	public void setEvent(ScheduleEvent event) {
		this.event = event;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}
}